package com.sayimo.maker.mall.orders.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sayimo.framework.exception.DaoException;
import com.sayimo.framework.exception.ServiceException;
import com.sayimo.maker.commons.Constant;
import com.sayimo.maker.mall.orders.dao.OrderPeriodInterfaceDao;
import com.sayimo.maker.mall.orders.service.OrderPeriodInterfaceService;



/**
 * @Project: schoolmallapi
 * @ClassName: OrderPeriodInterfaceImplService
 * @Description: 预约时间Service
 * @Author:	曹频辉
 * @Date:	日期：2016年1月8日  时间：下午6:05:46
 * @Version 1.0.0
 */
@Service(value = "orderPeriodInterfaceImplService")
public class OrderPeriodInterfaceImplService implements OrderPeriodInterfaceService {
	private static Log logger = LogFactory.getLog(OrderPeriodInterfaceImplService.class);
	@Autowired
	private OrderPeriodInterfaceDao orderPeriodInterfaceDao;
	
	
	
	public OrderPeriodInterfaceDao getOrderPeriodInterfaceDao() {
		return orderPeriodInterfaceDao;
	}



	public void setOrderPeriodInterfaceDao(
			OrderPeriodInterfaceDao orderPeriodInterfaceDao) {
		this.orderPeriodInterfaceDao = orderPeriodInterfaceDao;
	}



	/** 
	* @Title: queryOrderPeriodInfoByGoodsId 
	* @Description: 查询商品的预约时间信息
	* @throws ServiceException
	* @return Map<String,Object>
	* @param goodsId
	*/ 
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Map<String, Object> queryOrderPeriodInfoByGoodsId(Integer goodsId)
			throws ServiceException {
		Map map=new HashMap();
		try{
			Map templateMap=orderPeriodInterfaceDao.getOrderTemplateByGoodsId(goodsId);
			if(templateMap!=null){
				List<Map> list=orderPeriodInterfaceDao.getOrderPeriodsByTemplateId((Integer)(templateMap.get("periodsId")));
				templateMap.remove("periodsId");
				if(!templateMap.get("type").equals("0")){
					templateMap.put("quickId", -1);
				}else{
					templateMap.put("quickId", "");
				}
				if(list!=null&&list.size()>=0){
					for(Map periodMap:list){
						if(periodMap.get("periodsId")==null){
							periodMap.put("periodsId", "");
						}
						if(periodMap.get("startTime")==null){
							periodMap.put("startTime", "");
						}
						if(periodMap.get("endTime")==null){
							periodMap.put("endTime", "");
						}
					}
					templateMap.put("periods", list);
					
					return templateMap;
				}else{
					map.put(Constant.STATUS, Constant.STATUS_ERROR);
					map.put(Constant.STATUS_ERROR, Constant.SELECT_ERROR);
					return map;
				}
			}else{
				Map tempMap=new HashMap();
				
				tempMap.put("type", "");
				tempMap.put("quickId", "");
				tempMap.put("periods", new ArrayList());
				
				return tempMap;
			}
		}catch(DaoException de){
			logger.error(de);
			throw new ServiceException(de.getMessage());
		}
		
	}



	@Override
	public Map<String, Object> queryOrderPeriodByPeriodId(Integer id) throws ServiceException {
		try {
			return orderPeriodInterfaceDao.queryOrderPeriodByPeriodId(id);
		} catch (DaoException de) {
			throw new ServiceException(de);
		}
	}
	
}
